/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.activity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.activity.dto.CnActivityDTO;
import com.ykht.proj.activity.entity.CnActivity;
import com.ykht.proj.activity.entity.CnActivityShop;
import com.ykht.proj.activity.mapper.CnActivityMapper;
import com.ykht.proj.activity.service.CnActivityService;
import com.ykht.proj.activity.service.CnActivityShopService;
import com.ykht.proj.activity.vo.AboutActivityShopVo;
import com.ykht.proj.activity.vo.ActivityVo;
import com.ykht.proj.common.core.constant.CommonConstants;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.api.feign.RemoteShopService;
import com.ykht.proj.coupon.vo.AboutShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 活动表
 *
 * @author csg
 * @date 2020-12-23 11:13:32
 */
@Service
public class CnActivityServiceImpl extends ServiceImpl<CnActivityMapper, CnActivity> implements CnActivityService {

	@Autowired
	private CnActivityShopService cnActivityShopService;
	@Resource
	private RemoteShopService remoteShopService;

	@Resource
	private CnActivityMapper cnActivityMapper;

	/**
	 * 添加活动数据
	 *
	 * @param cnActivityDTO
	 */
	@Override
	public void addCnActivity(CnActivityDTO cnActivityDTO) {
		CnActivity cnActivity = BeanUtil.toBean(cnActivityDTO, CnActivity.class);
		boolean save = this.save(cnActivity);
		List<CnActivityShop> activityShopList = new ArrayList<>();
		for (String shopId : cnActivityDTO.getShopIds()) {
			CnActivityShop cnActivityShop = new CnActivityShop();
			cnActivityShop.setShopId(Long.valueOf(shopId));
			cnActivityShop.setActivityId(cnActivity.getId());
			cnActivityShop.setCreateUser(SecurityUtils.getUserId());
			cnActivityShop.setCreateTime(LocalDateTime.now());
			activityShopList.add(cnActivityShop);
		}
		boolean saveBatch = cnActivityShopService.saveBatch(activityShopList);
	}

	/**
	 * 修改活动表(并同时修改 商铺活动关联表)
	 *
	 * @param cnActivityDTO
	 */
	@Override
	public void updateCnActivityAndShopIds(CnActivityDTO cnActivityDTO) {
		// 修改活动
		CnActivity cnActivity = BeanUtil.toBean(cnActivityDTO, CnActivity.class);
		boolean update = this.updateById(cnActivity);
		// 删除 商铺活动关联表
		QueryWrapper<CnActivityShop> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(CnActivityShop::getActivityId, cnActivity.getId());
		boolean remove = cnActivityShopService.remove(queryWrapper);
		// 添加 商铺活动关联表
		List<CnActivityShop> activityShopList = new ArrayList<>();
		for (String shopId : cnActivityDTO.getShopIds()) {
			CnActivityShop cnActivityShop = new CnActivityShop();
			cnActivityShop.setShopId(Long.valueOf(shopId));
			cnActivityShop.setActivityId(cnActivity.getId());
			cnActivityShop.setCreateUser(SecurityUtils.getUserId());
			cnActivityShop.setCreateTime(LocalDateTime.now());
			activityShopList.add(cnActivityShop);
		}
		cnActivityShopService.saveBatch(activityShopList);
	}

	@Override
	public R<ActivityVo> getActivityById(Page page, Long id, String longitude, String latitude) {
		CnActivity cnActivity = this.getById(id);
		if (null == cnActivity) return R.failed("活动不存在");
		if (1 == cnActivity.getExpireStatus()) return R.failed("活动已过期");
		if (1 == cnActivity.getDisplay()) return R.failed("活动已下架");
		ActivityVo vo = new ActivityVo();
		vo.setCnActivity(cnActivity);

		R<IPage<AboutShopVo>> pageR = cnActivityShopService.getCnActivityShopPage(page, id, longitude, latitude);
		if (CommonConstants.SUCCESS == pageR.getCode()) {
			IPage<AboutActivityShopVo> actPage = new Page<>();
			BeanUtil.copyProperties(pageR.getData(), actPage);
			vo.setPage(actPage);
		}
		return R.ok(vo);
	}

	/**
	 * 根据ID 获取活动详情
	 *
	 * @param id
	 * @return
	 */
	@Override
	public CnActivityDTO getByIdCnActivity(Long id) {
		return cnActivityMapper.getByIdCnActivity(id);
	}
}
